<?php

class SiteController extends Controller
{
	public $layout = '//layouts/login';

	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'Lucifer',
                'backColor' => 0xFFFFFF,
                'maxLength' => 4, // 最多生成几个字符
                'minLength' => 4, // 最少生成几个字符
                //'fixedVerifyCode' => substr(md5(time()),0,4),
                // 'transparent' => true,//是否是背景透明
                'testLimit' => 0, //不限制相同验证码出现的次数
                'offset' => 5,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if( Yii::app()->errorHandler->error['code'] && !(strpos(Yii::app()->errorHandler->error['trace'], 'api/') === FALSE)){
				header('Content-type: application/json;charset=utf-8');
				$result = array('code'=>Yii::app()->errorHandler->error['code'] , 
					'msg'=> "参数名称错误或者数量不够",
					"data" => '');
				echo json_encode($result);
			}elseif(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}

	// 	public function actionError()
	// {
	// 	if($error=Yii::app()->errorHandler->error)
	// 	{
	// 		if(Yii::app()->request->isAjaxRequest)
	// 			echo $error['message'];
	// 		else
	// 			$this->render('error', $error);
	// 	}
	// }


	// /**
	//  * Displays the login page
	//  */
	public function actionLogin()
	{
		$model=new LoginForm;

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];

			// $model_verify = $model->verifyCode;
            // $model->verifyCode = "";
			
			// if ($this->createAction('captcha')->getVerifyCode() != $model_verify) {
                // $model->addError('password', '验证码错误!');
                // $this->render('login', array(
                //     'model' => $model,
                //     'login_back_img' => Yii::app()->request->baseUrl . "/img/loginback/login-" . rand(1, 3) . ".jpg",
                // ));
                // return;
            // }
			// validate user input and redirect to the previous page if valid
			if($model->login()){
				// echo $model->username;
				// return;
				$this->redirect(Yii::app()->user->returnUrl);
			}
		}

		// display the login form
		$this->render('login',array('model'=>$model,
			'login_back_img' => Yii::app()->request->baseUrl . "/img/loginback/login-" . rand(1, 3) . ".jpg"));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}